package com.jz.community.moduleshopping.coupon.adapter;

import android.support.annotation.Nullable;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jz.community.basecomm.utils.CharacterUtils;
import com.jz.community.basecomm.utils.ConverterUtils;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.basecomm.utils.RxTimeTool;
import com.jz.community.basecomm.utils.SHelper;
import com.jz.community.moduleshopping.R;
import com.jz.community.moduleshopping.confirmOrder.bean.BaseOrderCouponInfo;
import com.ms.square.android.expandabletextview.ExpandableTextView;

import java.util.List;


/**
 * Created by Tony.Fan on 2018/6/06 14:16
 * 优惠券列表
 */
public class CouponListAdapter extends BaseQuickAdapter<BaseOrderCouponInfo, BaseViewHolder> {
    private int coupon_type;

    public CouponListAdapter(int layoutResId, @Nullable List<BaseOrderCouponInfo> data, int coupon_type) {
        super(layoutResId, data);
        this.coupon_type = coupon_type;
    }

    @Override
    protected void convert(BaseViewHolder helper, BaseOrderCouponInfo entity) {
        LinearLayout top_ll = helper.getView(R.id.coupon_top_ll);
        LinearLayout bot_ll = helper.getView(R.id.coupon_bot_ll);
        TextView use_tv = helper.getView(R.id.coupon_list_click_tv);//立即使用
        TextView disable_tv = helper.getView(R.id.coupon_list_disable_tv);//已过期 已使用

        TextView coupon_gt_all = helper.getView(R.id.coupon_gt_all);

        ExpandableTextView ex_tv = helper.getView(R.id.coupon_gt_expand);

        StringBuilder sb = new StringBuilder();

        // historyStatus -》用户-优惠券现状标识(1-已失效,2-已使用,3-已领取未使用,4-未领取,5-未启用)
        //couponType  0 未使用、未启用 1 已使用
        if (coupon_type == 0) {
            use_tv.setBackgroundResource(R.drawable.border_coupon_btn);
            if (entity.getHistoryStatus() == 3) {
                SHelper.gone(disable_tv);
                SHelper.vis(use_tv);
                use_tv.setText("立即使用");
            } else if (entity.getHistoryStatus() == 5) {
                SHelper.gone(disable_tv);
                SHelper.vis(use_tv);
                use_tv.setText("未启用");
            }
        } else {
            SHelper.gone(use_tv);
            SHelper.vis(disable_tv);
            if (entity.getHistoryStatus() == 1) {
                disable_tv.setText("已失效");
            } else if (entity.getHistoryStatus() == 2) {
                disable_tv.setText("已使用");
            }
        }

        //优惠卷类型:1-现金卷 2-折扣卷 3-免单券
        if (entity.getCouponType() == 1) {
            //现金券
            helper.setText(R.id.coupon_list_price, CharacterUtils.roundByScale(ConverterUtils.toDouble(entity.getCouponAmount())));
            helper.setText(R.id.coupon_list_tv, "元");
            SHelper.vis(helper.getView(R.id.coupon_list_tv));
            if (entity.getMinFullAmount() > 0) {
                coupon_gt_all.setText("满¥" + CharacterUtils.roundByScale(entity.getMinFullAmount()) + "使用");
            } else {
                coupon_gt_all.setText("无金额门槛");
            }
        } else if (entity.getCouponType() == 2) {
            //折扣券
            double discountPrice = ConverterUtils.toDouble(entity.getCouponDiscount()) * 10;
            helper.setText(R.id.coupon_list_price, CharacterUtils.roundByScale(discountPrice));
            SHelper.vis(helper.getView(R.id.coupon_list_tv));
            helper.setText(R.id.coupon_list_tv, "折");
            if (entity.getMinFullAmount() > 0) {
                coupon_gt_all.setText("满¥" + entity.getMinFullAmount() + "使用");
            } else {
                coupon_gt_all.setText("无金额门槛");
            }
            //免单券
        } else if (entity.getCouponType() == 3) {
            helper.setText(R.id.coupon_list_price, CharacterUtils.roundByScale(ConverterUtils.toDouble(entity.getCouponAmount())));
            helper.setText(R.id.coupon_list_tv, "元");
            SHelper.gone(helper.getView(R.id.coupon_list_tv));
            coupon_gt_all.setText("免单卷");
            handleCouponItemInfo(entity, -1, sb, ex_tv, 3);
        }

        helper.setText(R.id.coupon_list_time, RxTimeTool.date2String(entity.getUseStartDate()) + "至" + RxTimeTool.date2String(entity.getUseEndDate()));


        helper.setText(R.id.coupon_list_rule, entity.getCouponName());

        switch (entity.getPlatformOrMerchant()) {

            case 0:
                //平台券适用范围
                if (entity.getCouponLimitShop() == 0) {
                    sb.append("全平台通用");
                    handleCouponItemInfo(entity, entity.getPlatformOrMerchant(), sb, ex_tv, 0);
                } else if (entity.getCouponLimitShop() == 1) {
                    sb.append("指定商家适用：");
                    handleCouponItemInfo(entity, entity.getPlatformOrMerchant(), sb, ex_tv, 0);
                } else if (entity.getCouponLimitShop() == 2) {
                    sb.append("指定类目适用：");
                    handleCouponItemInfo(entity, entity.getPlatformOrMerchant(), sb, ex_tv, 0);
                } else if (entity.getCouponLimitShop() == 3) {
                    sb.append("指定商品适用：");
                    handleCouponItemInfo(entity, entity.getPlatformOrMerchant(), sb, ex_tv, 0);
                }
                break;
            case 1:
                //商家卷适用范围
                if (entity.getCouponLimitGoods() == 0) {
                    if (!Preconditions.isNullOrEmpty(entity.getShopName())) {
                        sb.append("指定店铺适用：" + entity.getShopName());
                    }
                    handleCouponItemInfo(entity, entity.getPlatformOrMerchant(), sb, ex_tv, 1);
                } else if (entity.getCouponLimitGoods() == 1) {
                    if (!Preconditions.isNullOrEmpty(entity.getShopName())) {
                        sb.append("指定店铺适用：" + entity.getShopName());
                        sb.append("\n");
                    }
                    sb.append("指定商品适用：");
                    handleCouponItemInfo(entity, entity.getPlatformOrMerchant(), sb, ex_tv, 1);
                } else if (entity.getCouponLimitGoods() == 2) {
                    if (!Preconditions.isNullOrEmpty(entity.getShopName())) {
                        sb.append("指定店铺适用：" + entity.getShopName());
                        sb.append("\n");
                    }
                    sb.append("指定类目适用：");
                    handleCouponItemInfo(entity, entity.getPlatformOrMerchant(), sb, ex_tv, 1);
                }
                break;
        }

        setCouponBackground(top_ll, bot_ll, entity);
        SHelper.vis(bot_ll);

        //点击事件
        helper.addOnClickListener(R.id.coupon_list_click_tv);

    }

    /**
     * 显示不同平台卷、店铺卷
     *
     * @param entity
     * @param platformOrMerchant
     * @param sb
     * @param ex_tv
     */
    private void handleCouponItemInfo(BaseOrderCouponInfo entity, int platformOrMerchant, StringBuilder sb, ExpandableTextView ex_tv, int couponType) {
        if (platformOrMerchant == 0) {
            if (!Preconditions.isNullOrEmpty(entity.getCouponItemList())) {
                for (BaseOrderCouponInfo.CouponItemListBean couponItemListBean : entity.getCouponItemList()) {
                    if (!Preconditions.isNullOrEmpty(couponItemListBean.getGoodsName())) {
                        sb.append(couponItemListBean.getGoodsName());
                        sb.append(" ");
                    }
                    if (!Preconditions.isNullOrEmpty(couponItemListBean.getGoodsCategoriesName())) {
                        sb.append(couponItemListBean.getGoodsCategoriesName());
                        sb.append(" ");
                    }
                }
                sb.append("\n");
            }
        } else if (platformOrMerchant == 1) {
            if (!Preconditions.isNullOrEmpty(entity.getCouponItemList())) {
                for (BaseOrderCouponInfo.CouponItemListBean couponItemListBean : entity.getCouponItemList()) {

                    if (!Preconditions.isNullOrEmpty(couponItemListBean.getGoodsName())) {
                        sb.append(couponItemListBean.getGoodsName());
                        sb.append(" ");
                    }

                    if (!Preconditions.isNullOrEmpty(couponItemListBean.getGoodsCategoriesName())) {
                        sb.append(couponItemListBean.getGoodsCategoriesName());
                        sb.append(" ");
                    }
                }
                sb.append("\n");
            }
        } else if (couponType == 3) {
            if (!Preconditions.isNullOrEmpty(entity.getCouponItemList())) {
                for (BaseOrderCouponInfo.CouponItemListBean couponItemListBean : entity.getCouponItemList()) {

                    if (!Preconditions.isNullOrEmpty(couponItemListBean.getGoodsName())) {
                        sb.append(couponItemListBean.getGoodsName());
                        sb.append(" ");
                    }

                    if (!Preconditions.isNullOrEmpty(couponItemListBean.getGoodsCategoriesName())) {
                        sb.append(couponItemListBean.getGoodsCategoriesName());
                        sb.append(" ");
                    }
                }
                sb.append("\n");
            }
        }
        if (!Preconditions.isNullOrEmpty(sb.toString()) && !Preconditions.isNullOrEmpty(entity.getCouponItemList())) {
            ex_tv.setText(sb.substring(0, sb.length() - 1));
        } else {
            ex_tv.setText(sb.substring(0, sb.length()));
        }
    }

    private void setCouponBackground(LinearLayout top_ll, LinearLayout bot_ll, BaseOrderCouponInfo entity) {
        if (coupon_type == 0) {//可用
            if (entity.getHistoryStatus() == 3) {
                top_ll.setBackgroundResource(R.drawable.border_coupon_red_top);
                bot_ll.setBackgroundResource(R.drawable.border_coupon_red_bot);
            } else if (entity.getHistoryStatus() == 5) {
                top_ll.setBackgroundResource(R.drawable.border_coupon_red_top);
                bot_ll.setBackgroundResource(R.drawable.border_coupon_red_bot);
            }

        } else {
            top_ll.setBackgroundResource(R.drawable.border_coupon_gray_top);
            bot_ll.setBackgroundResource(R.drawable.border_coupon_gray_bot);
        }
    }
}
